<!-- 上传文件 -->
<template>
  <div class="upload-box">
    <el-form-item label-width="90">
      <template #label>
        <span style="padding-right: 5px; color: red">*</span>
        <span class="form-label">{{ $t('smt.BOMFile') }}</span>
      </template>
      <div class="content-wrap">
        <el-upload :before-upload="uploadChange">
          <el-button class="upload-btn" type="primary">{{ props.name }}</el-button>
        </el-upload>
        <div class="select-btn">
          <slot name="select-btn"></slot>
        </div>
      </div>
      <div class="upload-tips-wrap">
        <p class="upload-tips" v-html="$t('mall.upload_file_support')" />
      </div>
      <el-link :underline="false" @click="downloadBom" type="primary"
        >{{ t('mall.file_standard_template') }}
      </el-link>
    </el-form-item>
    <slot></slot>
  </div>
</template>
<script setup>
import { ref } from 'vue';
import { standardDocumentsPath } from '@/server/api/file';
import { downloadFile } from '@/utils/common.js';
import { useI18n } from 'vue-i18n';
const { t } = useI18n();

const visible = ref(false);
const props = defineProps({
  fileType: {
    type: Number,
  },
  quantity: {
    type: Number,
  },
  name: {
    type: String,
  },
  type: {
    type: String,
  },
  smdNum: {
    type: Number,
  },
});
const emit = defineEmits(['getModea', 'getFlieData', 'scrollToSection']);

/**
 * 获取file文件
 * @param {*} file
 */
const uploadChange = (file) => {
  console.log('文件上传file', file, props.fileType);
  if (props.fileType == '2') {
    if (file.size > 50 * 1024 * 1024) {
      ElMessage.error(t('mall.upload_file_limit', { num: 50 }));
      return false;
    }
    visible.value = true;
    emit('getFlieData', file);
    emit('getModea', true);
  }

  if (props.fileType == '1') {
    if (file.size > 50 * 1024 * 1024) {
      ElMessage.error(t('mall.upload_file_limit', { num: 50 }));
      return false;
    }
    if (!props.smdNum) {
      ElMessage.error(t('mall.select_smt_quantity'));
      const el = '#basicInfor';
      emit('scrollToSection', el);
      return false;
    }
    emit('getFlieData', file);
  }
};

/**
 * 下载BOM文件模板
 */
const downloadBom = () => {
  standardDocumentsPath().then((res) => {
    if (res.code == 200) {
      downloadFile(res.data.bomLinkPath);
    }
  });
};
</script>
<style lang="scss" scoped>
.upload-box {
  border: 1px solid #d9d9d9;
  height: 140px;
  border-radius: 8px;
  background: #f5f5f5;
  padding: 20px;
  .form-label {
    color: #383838;
    font-size: 14px;
  }
  ::v-deep .el-form-item__content {
    flex-direction: column;
  }
  ::v-deep .el-button {
    font-weight: 400;
    padding: 0 25px !important;
  }

  ::v-deep .el-link {
    font-size: 12px;
  }
  .content-wrap {
    display: flex;
    .select-btn {
      margin-left: 10px;
    }
  }

  .upload-tips {
    text-align: center;
    width: 340px;
    color: #666666;
    font-size: 10px;
    line-height: 2;
    .accept {
      color: #ff0000;
    }
  }
}
</style>
